package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.domain.ExamBank;
import com.ruoyi.system.service.IExamBankService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ExamPaper;
import com.ruoyi.system.service.IExamPaperService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷Controller
 * 
 * @author ruoyi
 * @date 2023-07-30
 */
@Controller
@RequestMapping("/system/paper")
public class ExamPaperController extends BaseController
{
    private String prefix = "system/paper";

    @Autowired
    private IExamPaperService examPaperService;

    @Autowired
    private IExamBankService examBankService;

    @RequiresPermissions("system:paper:view")
    @GetMapping()
    public String paper()
    {
        return prefix + "/paper";
    }

    /**
     * 查询试卷列表
     */
    @RequiresPermissions("system:paper:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamPaper examPaper)
    {
        startPage();
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        return getDataTable(list);
    }

    /**
     * 导出试卷列表
     */
    @RequiresPermissions("system:paper:export")
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamPaper examPaper)
    {
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        ExcelUtil<ExamPaper> util = new ExcelUtil<ExamPaper>(ExamPaper.class);
        return util.exportExcel(list, "试卷数据");
    }

    /**
     * 新增试卷
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存试卷
     */
    @RequiresPermissions("system:paper:add")
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamPaper examPaper)
    {
        return toAjax(examPaperService.insertExamPaper(examPaper));
    }

    /**
     * 修改试卷
     */
    @RequiresPermissions("system:paper:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamPaper examPaper = examPaperService.selectExamPaperById(id);
        mmap.put("examPaper", examPaper);
        return prefix + "/edit";
    }

    /**
     * 修改保存试卷
     */
    @RequiresPermissions("system:paper:edit")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamPaper examPaper)
    {
        return toAjax(examPaperService.updateExamPaper(examPaper));
    }

    /**
     * 删除试卷
     */
    @RequiresPermissions("system:paper:remove")
    @Log(title = "试卷", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examPaperService.deleteExamPaperByIds(ids));
    }

    @RequiresPermissions("system:paper:edit")
    @GetMapping("/selectBank")
    public String selectBank(String paperId, Model model){
        //加载题库
        //List<ExamBank>  banks = examBankService.selectExamBankList(null);
        //查找已经选择的试题
        List<Integer> selectedBank = examPaperService.selectBankByPaperId(paperId);

        //model.addAttribute("banks",banks);
        model.addAttribute("selectedBank",selectedBank);
        model.addAttribute("id",paperId);
        return prefix + "/selectBank";
    }

    @RequiresPermissions("system:paper:edit")
    @Log(title = "试卷修改试题", businessType = BusinessType.DELETE)
    @PostMapping("/changeBank")
    @ResponseBody
    public AjaxResult changeBank(String id, Model model,String selectedBank){

        String[] ints = selectedBank.split(",");
        Integer[] i = new Integer[ints.length];
        for (int j = 0; j < ints.length; j++) {
            i[j] = Integer.valueOf(ints[j]);
        }
        return toAjax(examPaperService.changeBank(id,i));
    }
}
